import axios from 'axios'
import { MessageBox } from 'element-ui';

const service = axios.create({
  timeout: 60000 // 请求超时时间
})

service.interceptors.request.use(
  config => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers['Authorization'] = token
    }
    return config
  },
  err => {
    return Promise.reject(err)
  }
)
service.interceptors.response.use(
  res => {
    if (res.data.code === 9997) {
      MessageBox.alert('登录失效，请重新登录', '提示', {
        confirmButtonText: '确定',
        confirmButtonClass: 'tipsConfirm',
        callback: (action) => {
          if (action === 'confirm') {
            window.location.href = '/'
          }
        }
      });
      return Promise.reject(res)
    } else if (res.data.code === 9999) {
      MessageBox.alert(res.data.msg, '提示', {
        confirmButtonText: '确定',
        confirmButtonClass: 'tipsConfirm'
      });
      // return { data: {} }
      return Promise.reject(res)
    } else {
      return res.data
    }

  },
  err => {
    debugger
    return Promise.reject(err)
  }
)

export default service